package cn.whx.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration //作为一个配置类
public class HrmOAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
    //密码解析器
    @Bean
    public PasswordEncoder getEncoder(){
        return new BCryptPasswordEncoder();
    }
    //配置http安全配置

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                /*.antMatchers("/login").permitAll()//登录接口放行*/
                .antMatchers("/loginUser/login").permitAll()//登录接口放行
                .antMatchers("/loginUser/refresh").permitAll()//自己的登录接口
                .anyRequest().authenticated()//其他请求都需要认证
                .and().formLogin() //允许表单登录
                .successForwardUrl("/loginUser/success")

                .and().logout().permitAll()
                .and().csrf().disable();//关闭跨站
    }

    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
